package com.kukudexin;

/**
 * @author: 德鑫
 * @description: 测试jdk 14新特性
 *   switch表达式的增强
 *    case L -> 标签(不带有贯穿功能)
 *    switch新增yield语句
 * @since: 2021/4/29
 */
public class TestJdk1401 {
    public static void main(String[] args) {
        Weekday day = Weekday.MON;	//初始化一个枚举变量
        switch (day){
            case MON:
            case TUE:
            case WEN:
                String s = "";			//case L : 标签中, 局部变量是在整个switch中有效
                System.out.println("上半周");
            case THU:
            case FRI:
                //String s = "def";		//重复定义
                System.out.println("下半周");
            case SAT:
            case SUN:
                System.out.println("周末");
        }
        System.out.println("--------");
        //1) case L -> 标签没有贯穿功能, case后面可以使用逗号分隔多个常量
        switch( day ){
            case MON,TUE, WEN	-> {System.out.println("上半周");	String s = "abc";}
            case THU,FRI		-> {System.out.println("下半周");	String s = "def";}
            case SAT, SUN		-> {System.out.println("周末");		String s = "hehe";}
        }
        System.out.println("--------");

        //2) JDK14扩展了switch可以作为表达式
        System.out.println( switch(day){
            case MON,TUE, WEN	-> "上半周";
            case THU,FRI		-> "下半周";
            case SAT, SUN		-> "周末";
        });
        System.out.println("--------");

         String text = switch (day){
            case MON,TUE, WEN	-> "上半周";
            case THU,FRI		-> "下半周";
            case SAT, SUN		-> "周末";
        };
        System.out.println(text);

        //3) 通过yield产生值
        day = Weekday.SAT;
        int x = switch( day ) {
            case MON,TUE, WEN	-> 1;
            case THU,FRI		-> 2;
            case SAT, SUN		->{
                int  rnd = (int)(Math.random() * 10 );	//生成[0,10)范围内的随机整数
                System.out.println("rnd=" + rnd);
                yield rnd;		//通过yield产生一个新值
            }
        };
        System.out.println("x=" + x);

    }
}

//定义枚举类型
enum  Weekday{
    MON,TUE,WEN,THU,FRI,SAT,SUN
}